package com.fly.autogetcode;

import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

/**
 * Created by weifeiyang on 2016/7/29 0029.
 */
public class CommonActivity extends AppCompatActivity {

    @Bind(R.id.login_userPhone_text)
    EditText userPhoneEditText;//手机号编辑

    @Bind(R.id.login_sendSecode_btn)
    Button secodeBtn;//获取验证码

    @Bind(R.id.user_seCode_text)
    EditText seCodeEditText;//验证码编辑

    @Bind(R.id.title_tv)
    TextView titleTv;//标题

    private ReadSmsContent mReadSmsContent;

    private SMSBroadcastReceiver mSMSBroadcastReceiver;

    private TimeCountUtil time;//倒计时

    private String flag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);
        ButterKnife.bind(this);
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        switch (flag) {
            case "broad_cast"://广播
                //注销短信监听广播
                this.unregisterReceiver(mSMSBroadcastReceiver);
                break;

            case "content_oberver"://数据库
                //注销内容监听者
                this.getContentResolver().unregisterContentObserver(mReadSmsContent);
                break;
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {

        flag = getIntent().getExtras().getString("flag");
        switch (flag) {
            case "broad_cast"://广播
                titleTv.setText("广播方式");
                initSMSBroadcastReceiver();
                break;

            case "content_oberver"://数据库
                titleTv.setText("数据库方式");
                initSmSContentObserver();
                break;
        }
    }

    /**
     * <pre>
     *   获取验证码
     * </pre>
     */
    @OnClick(R.id.login_sendSecode_btn)
    public void sendSecodeBtn() {
        sendSeCode();
    }

    /**
     * <pre>
     *  发送验证码参数
     * </pre>
     */
    private void sendSeCode() {

        RequestParams params = new RequestParams();
        params.put("phoneNum", userPhoneEditText.getText().toString());
        params.put("smsType", "AUTH");
        String partUrl = "http://120.25.195.84/apis/1/sms/identifyingcode/mobile?";
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(partUrl, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.e("onSuccess", "onSuccess" + new String(responseBody));
                time = new TimeCountUtil(60000, 1000,secodeBtn);
                time.start();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

    /**
     * 初始化短信监听广播
     */
    private void initSMSBroadcastReceiver() {
        //生成广播处理
        mSMSBroadcastReceiver = new SMSBroadcastReceiver();
        //实例化过滤器并设置要过滤的广播
        IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION);
        //优先级最高
        intentFilter.setPriority(Integer.MAX_VALUE);
        //注册广播
        this.registerReceiver(mSMSBroadcastReceiver, intentFilter);
        mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void onReceived(String message) {
                Log.e("message", message);
                String code = ReadSmsContent.getDynamicPassword(message);
                seCodeEditText.setText(code);
            }
        });
    }

    /**
     * 初始化短信监听数据库
     */
    private void initSmSContentObserver() {
        mReadSmsContent = new ReadSmsContent(new Handler(), this, seCodeEditText);
        //注册短信内容监听
        this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, mReadSmsContent);

    }
}
